<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="sec"
	uri="http://www.springframework.org/security/tags"%>
<%@include file="../common/jspcommon.jsp"%>
<%@include file="../common/jscss.jsp"%>
<script type="text/javascript">
	var grid_ae = null;
	var dialog_ae = null;
	var contentList = [];
	$(function() {
		grid_ae = $("#discuss_grid")
				.ligerGrid(
						{
							checkbox : true,
							selectRowButtonOnly : true,
							columns : [
									{
										display : '<a href="javascript:;" onclick="pushDiscuss()">推至频道</a>',
										width : 50
									},
									{
										display : '标识',
										id : 'identifier',
										isSort : false,
										width : 200,
										render : function(record, rowindex,
												value, column) {
											var html = '<a onclick="viewDisTag(\''
													+ record.identifier
													+ '\')" href="javascript:;">'
													+ record.identifier
													+ '</a>';
											return html;
										}
									},
									{
										display : '话题标题',
										name : 'title',
										isSort : false,
										width : 200,
										render : function(record, rowindex,
												value, column) {
											var html = '<a onclick="detail(\''
													+ record.identifier
													+ '\')" href="javascript:;">'
													+ record.title + '</a>';
											return html;
										}
									},
									{
										display : '标题图片',
										name : 'titlePic',
										isSort : false,
										width : 100
									},
									{
										display : '创建人',
										name : 'creator',
										isSort : false,
										width : 80
									},
									{
										display : '访问数',
										name : 'visitAmout',
										isSort : false,
										width : 50
									},
									{
										display : '回复数',
										name : 'replyAmout',
										isSort : false,
										width : 50
									},
									{
										display : '是否允许回复',
										name : 'isReply',
										isSort : false,
										width : 80
									},
									{
										display : '是否允许回复图片',
										name : 'isReplyPic',
										isSort : false,
										width : 100
									},
									{
										display : '主题',
										name : 'theme',
										isSort : false,
										width : 80,
										render : function(record, rowindex,
												value, column) {
											var html = '';
											if (value == '') {
												html = '无';
											} else if (value == '01') {
												html = '图片主题';
											} else if (value == '02') {
												html = '商品主题';
											}
											return html;
										}
									},
									{
										display : '内容',
										name : 'content',
										isSort : false,
										width : 200
									},
									{
										display : '创建时间',
										name : 'gmtCreated',
										isSort : false,
										width : 100,
										render : function(record, rowindex,
												value, column) {
											return (new Date(value.time))
													.format("yyyy年MM月dd日 hh:mm:ss");
										}
									},
									{
										display : '<a href="javascript:;" onclick="newDiscuss()">新增文稿</a>',
										isAllowHide : false,
										isSort : false,
										render : function(record, rowindex,
												value, column) {
											var html = ' <a onclick="deleteDis(\''
													+ record.identifier
													+ '\')" href="javascript:;">删除</a> ';
											return html;
										}
									} ],
							pageSize : 10,
							root : 'list',
							record : 'totalRecord',
							url : "/misc/discuss/disgrid.htm",
							onCheckAllRow : f_onCheckAllRow,
							isChecked : f_isChecked,
							onCheckRow : f_onCheckRow,
							width : '99%',
							height : '70%'
						});
	});

	function newDiscuss() {
		dialog_ae = $.ligerDialog.open({
			height : 600,
			width : 900,
			url : "/misc/discuss/newdis.htm",
			isResize : false,
			modal : true,
			title : '添加新稿件'
		});
	}
	function viewDisTag(id) {
		dialog_ae = $.ligerDialog.open({
			height : 600,
			width : 900,
			url : "/misc/discuss/viewdistag.htm?id=" + id,
			isResize : false,
			modal : true,
			title : '查看标签'
		});
	}
	//推送
	function pushDiscuss() {
		if (contentList.length == 0) {
			$.ligerDialog.warn("请选择推送项！");
			return;
		}
		$("#channel_discuss").show();
		var ct = $("#channel_table");
		var ct_row_td = ct.find("tr.row1").find("td");
		ct_row_td.css({
			width : ct.width() / ct_row_td.length + "px"
		});
	}

	function f_onCheckAllRow(checked) {
		for ( var rowid in this.records) {
			if (checked)
				addCheckedCustomer(this.records[rowid]);
			else
				removeCheckedCustomer(this.records[rowid]);
		}
	}
	function findCheckedCustomer(data) {
		for ( var i = 0; i < contentList.length; i++) {
			if (contentList[i] == data)
				return i;
		}
		return -1;
	}
	function addCheckedCustomer(data) {
		if (findCheckedCustomer(data) == -1) {
			contentList.push(data);
		}
	}
	function removeCheckedCustomer(data) {
		var i = findCheckedCustomer(data);
		if (i == -1)
			return;
		contentList.splice(i, 1);
	}
	function f_isChecked(rowdata) {
		if (findCheckedCustomer(rowdata) == -1)
			return false;
		return true;
	}
	function f_onCheckRow(checked, data) {
		if (checked) {
			addCheckedCustomer(data);
		} else {
			removeCheckedCustomer(data);
		}
	}

	function removeCheckedata() {
		for ( var i = 0; i < contentList.length; i++) {
			removeCheckedCustomer(contentList[i]);
		}
	}
	function detail(id) {
		dialog_ae = $.ligerDialog.open({
			height : 700,
			width : 1200,
			url : "/misc/discuss/discusstl.htm?id=" + id,
			showMax : true,
			showMin : true,
			isResize : true,
			isDrag : false,
			title : '文稿详情'
		});
	}
	//给推送时添加标签
	function disTag(channel, tagId, tagName) {
		// alert(channel);
		var tagchannel = $("#tag_" + channel);
		tagchannel
				.append('<span style="color: red; cursor: pointer; "  tagId="'+tagId+'" class="tags">'
						+ tagName + '</span>&nbsp;| ');
	}
	//删除
	function deleteDis(id) {
		$.ligerDialog.confirm('你真的要删除吗？', function(yes) {
			if (yes) {
				$.post("/misc/discuss/deleteDis.htm", {
					id : id
				}, function(data) {
					if (data == 'true') {
						window.location.href = "/misc/discuss/discuss.htm";
					} else {
						alert("失败了，你要联系马克扎克伯格了。");
					}
				});
			}
		});
	}

	//频道推送
	function pushDisBychannel(obj) {
		var channel = $(obj).attr("name");
		//alert(channel);
		
		var tagsList = new Array();
		$.ligerDialog.confirm('你真的要推送吗？', function(yes) {
			if (yes) {
				$(".tags").each(function() {
					tagsList.push({
						"discussTag" : $(this).attr("tagId")
					});
				});
				//alert( tagsList);
				$.post("/misc/discuss/pushdis.htm", {
					contentList : $.toJSON(contentList),
					tagsList : $.toJSON(tagsList),
					channel : channel
				}, function(data) {
					if (data == 'true') {
						$.ligerDialog.success("推送成功！");
						window.location.href = "/misc/discuss/discuss.htm";
						//loadDate();
					}else if(data=='pushErorr'){
						alert("图片话题推错频道了阿，小心阿。");
					} else {
						alert("失败了，你要联系马克扎克伯格了。");
					}

				});
			}
		});
	}
</script>
<style type="text/css">
.l-table-edit {
	font-size: 12px;
	margin: auto;
	width: 100%;
	border-collapse: collapse;
}

.l-table-edit td {
	padding: 4px;
	border: 1px solid #336699;
}
</style>
<div id="discuss_grid" style="margin: 0; padding: 0"></div>
<div id="channel_discuss" style="display: none;">

	<table class="l-table-edit" id="channel_table">
		<tr>
			<td></td>
			<c:forEach items="${channel}" var="cel">
				<c:if test="${cel!='77aa590e-c6e4-4183-9c09-e3a210480251'}">
					<td id="${cel}"><c:forEach items="${disTagList }" var="tags">
							<c:if test="${tags.channel==cel}">
								<span style="color: red; cursor: pointer;"
									onclick="disTag('${tags.channel}','${tags.identifier}','${tags.name}');">
									${tags.name}</span>&nbsp;| 
									</c:if>
						</c:forEach></td>
				</c:if>
			</c:forEach>

		</tr>


		<tr class="row1">
			<td><input type="button" value="整站文稿"
				onclick="pushDisBychannel(this);"
				name="b9a62576-070f-4c4d-8b8b-22389a8b626e" style="width: 150px;" />
			</td>
			<c:forEach items="${channel}" var="cel">
				<c:if test="${cel!='77aa590e-c6e4-4183-9c09-e3a210480251'}">
					<td><input type="button"
						value="<c:if test="${cel=='c1fd788e-590b-4579-b5ef-d21a9f42c92d'}">怀孕期</c:if><c:if test="${cel=='5e02f483-87bd-4d77-81d5-a64ecdc877d0'}">0-1岁</c:if><c:if test="${cel=='1d48a3b9-3709-4270-81b5-0267a5b3546f'}">1-3岁</c:if><c:if test="${cel=='b22c5b02-baa2-42de-ac92-09042e8ccfa2'}">3-6岁</c:if>"
						name="${cel}" onclick="pushDisBychannel(this);"
						style="width: 150px;" /></td>
				</c:if>
			</c:forEach>
		</tr>

		<tr>
			<c:forEach items="${channel}" var="cel">
				<td id="tag_${cel}"></td>
			</c:forEach>
		</tr>

	</table>

</div>
